package main

import (
	"Demo_Gin/middleware"
	"Demo_Gin/view"
	"embed"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
)

var _ embed.FS

// $env:GOOS = "windows"; $env:GOARCH = "amd64"; go build -o build/demo-gin.exe main.go

func init() {
	fmt.Println("init ...")

	gin.SetMode(gin.ReleaseMode)

	gin.DefaultWriter = io.Discard
	gin.DisableConsoleColor() // 禁止 Gin 的日志打印
}

func main() {
	r := gin.Default()

	//指定模板目录
	r.LoadHTMLGlob("templates/*")
	//指定静态文件目录
	//r.Static("/static", "./static")
	// r.StaticFile("/favicon.ico", "./favicon.ico")

	//全局的中间件
	r.Use(middleware.MaxBytesLimit(10 * middleware.MB))

	ViewRoute(&r.RouterGroup)
	Route(&r.RouterGroup)

	err := r.Run(":8080")
	if err != nil {
		return
	}
}

// ViewRoute 页面请求路由
func ViewRoute(router *gin.RouterGroup) {
	router.GET("/", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", nil)
	})
}

// Route 接口请求路由
func Route(router *gin.RouterGroup) {
	router = router.Group("/api")

	//可以传入多个全局中间件
	router.Use(middleware.AccessMonitor, middleware.MockSetLoginInfo)

	router.GET("/query/:userId", view.QueryData)
	router.POST("/form/:userId", view.FormSubmit)
	router.POST("/body/:userId", view.BodySubmit)

	router.POST("/upload", view.FileUpload)
	router.GET("/download", view.FileDownload)

	router.GET("/tree", view.GetTreeData)
	router.POST("/flow/callback", view.FlowCallback)

}
